package com.example.mybatis.service.impl;

import com.example.mybatis.entity.Record;
import com.example.mybatis.entity.Result;
import com.example.mybatis.entity.Trade;
import com.example.mybatis.mapper.RecordMapper;
import com.example.mybatis.mapper.ResultMapper;
import com.example.mybatis.mapper.TradeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.function.BiConsumer;

@Service
@Slf4j
public class RollbackService {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public void rollback() {

        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        try {
            String id = RandomStringUtils.randomNumeric(10);
            BiConsumer<String, SqlSession> consumer = tradeSave()
                    .andThen(recordSave())
                    .andThen(ResultSave());

            consumer.accept(id, sqlSession);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
        }


    }

    private BiConsumer<String, SqlSession> ResultSave() {
        return (id, sqlSession) -> {
            Result result = new Result();
            result.setId(id);
            result.setStatus("1");
            ResultMapper resultMapper = sqlSession.getMapper(ResultMapper.class);
            resultMapper.insert(result);
            String numeric = RandomStringUtils.randomNumeric(1);
            int anInt = Integer.parseInt(numeric);
            log.info("回滚数字：{}", anInt);
            if (anInt > 4) {
                int i = 10 / 0;
            }

        };
    }

    private BiConsumer<String, SqlSession> recordSave() {
        return (id, sqlSession) -> {
            Record record = new Record();
            record.setId(id);
            record.setRecordTime(new Date());
            RecordMapper recordMapper = sqlSession.getMapper(RecordMapper.class);
            recordMapper.insert(record);
        };
    }

    private BiConsumer<String, SqlSession> tradeSave() {
        return (id, sqlSession) -> {
            String content = "Spring Boot 3.0 现已正式发布，它包含了 12 个月以来 151 个开发者的 5700 多次代码提交。" +
                    "这是自 4.5 年前发布 2.0 以来，Spring Boot 的第一次重大修订。" +
                    "它也是第一个支持 Spring Framework 6.0 和 GraalVM 的 Spring Boot GA 版本，" +
                    "同时也是第一个基于Java 17的Spring Boot版本，以Jakarta EE 9为基准并支持Jakarta EE 10。" +
                    "同时借助于GraalVM支持了AOT和Native Image。";

            Trade trade = new Trade();
            trade.setId(id);
            trade.setContent(content);
            TradeMapper tradeMapper = sqlSession.getMapper(TradeMapper.class);
            tradeMapper.insert(trade);
        };
    }
}
